package fast.loader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

public class ObjectDB<E> {
	
	File file;
	
	public ObjectDB(String file){
		this.file = new File("db/"+file+".ser");
	}
	
	public void serialize(HashMap<Integer,E> db) {	
    	try	{  	
    		ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(file));
      		o.writeObject(db);
      		o.close();
    	}
    	catch(IOException e){ 
    		e.printStackTrace();
    	}
  	}
  	
  	@SuppressWarnings("unchecked")
	public HashMap<Integer, E> deserialize() {
  		HashMap<Integer,E> db = null;
  		try	{	
  			ObjectInputStream o = new ObjectInputStream(new FileInputStream(file));
  			db = (HashMap<Integer,E>) o.readObject();
      		o.close();
      		//System.out.println(db);
      	}
    	catch(Exception e){ 
    		e.printStackTrace();
    	}
    	return db;
  	}
  	
  	public boolean exists(){
  		return this.file.exists();
  	}
}
